import axios from 'axios';
import { useAuthStore } from "@/store";

const baseURL = import.meta.env.VITE_API_URL

const $http = axios.create({
    baseURL,
    timeout: 1000 * 12
});


// 请求前拦截器
$http.interceptors.request.use(
    async config => {
        const authStore = useAuthStore();
        const token = authStore.token || '';
        config.headers = {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json;charset=UTF-8',
        };
        return config;
    },
    error => {
        //请求错误时做些事
        return Promise.reject(error);
    }
);

// 请求后拦截
$http.interceptors.response.use(
    response => {
        const res = response.data
        return res;
    },
    error => {
        console.log(error);
        return Promise.reject(msg)
    }
);

export default $http;
